package com.letu.jwt.service;

import com.alibaba.fastjson.JSONObject;
import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.letu.core.context.ContextConstants;
import com.letu.jwt.JwtProperties;
import com.letu.jwt.TokenUtil;
import com.letu.jwt.model.AuthInfo;
import com.letu.jwt.model.JwtUserInfo;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

@Service
public class JwtService {

    private JwtProperties properties = new JwtProperties();
    static final TokenUtil tokenUtil = new TokenUtil(new JwtProperties());

    /**
     * 创建token
     *
     * @param user
     * @return
     */
    public AuthInfo build(JwtUserInfo user) {

//        String basic = "enVpaG91X2FkbWluX3VpenVpaG91X2FkbWluX3VpenVpaG91X2FkbWluX3VpenVpaG91X2FkbWluX3VpOnp1aWhvdV9hZG1pbl91aV9zZWNyZXR6dWlob3VfYWRtaW5fdWl6dWlob3VfYWRtaW5fdWl6dWlob3VfYWRtaW5fdWl6dWlob3VfYWRtaW5fdWl6dWlob3VfYWRtaW5fdWk=";
        //lamp_web:lamp_web_secret
//        String basic = "enVpaG91X3VpOnp1aWhvdV91aV9zZWNyZXQ=";
//        String basic ="enVpaG91X2FkbWluX3VpOnp1aWhvdV9hZG1pbl91aV9zZWNyZXQ=";

//        JwtUserInfo user = new JwtUserInfo();
//        user.setAccount("姓名长度不能超过50姓名长度不能超过50姓名长度");
//        user.setUserId(Long.MAX_VALUE);
//        user.setName("姓名长度不能超过50姓名长度不能超过50姓名长度");
//        user.setProjectId(111222L);
        AuthInfo authInfo = tokenUtil.createAuthInfo2(user, null);
        return authInfo;
    }

    public static void main(String[] args) {
//        JwtUserInfo jwtUserInfo = new JwtUserInfo();
//        jwtUserInfo.setUserId(123123123123L);
//        AuthInfo authInfo = new JwtService().build(jwtUserInfo);
//        System.out.println(JSONObject.toJSONString(authInfo));
//        AuthInfo authInfo1 = new JwtService().parse(authInfo.getToken());
//        System.out.println(JSONObject.toJSONString(authInfo1));
//        AuthInfo authInfo2 = new JwtService().parse("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VySWQiOjEyMzEyMzEyMzEyM30.OFyTLp-b8Fvm1jZAX3B7qdkmjRO6xTHn1NVz_4kf3hc");
//        System.out.println(JSONObject.toJSONString(authInfo2));


        Map<String, Object> map = new HashMap<>();
        map.put("JWT","HS256");
        Calendar instance = Calendar.getInstance();
        instance.add(Calendar.SECOND, 2000);

        String token = JWT.create()
//                .withHeader(map) //header
                .withClaim("userId", 123123123123L)//payload
                .withClaim("username", "测温点")//payload
//                .withExpiresAt(instance.getTime())//指定令牌的过期时间
                .sign(Algorithm.HMAC256(ContextConstants.JWT_SIGN_KEY)) //签名
                ;
        System.out.println(token);

        JWTVerifier jwtVerifier = JWT.require(Algorithm.HMAC256(ContextConstants.JWT_SIGN_KEY)).build();
        DecodedJWT decodedJWT = jwtVerifier.verify(token);
        System.out.println("用户Id：" + decodedJWT.getClaim("userId").asLong());
        System.out.println("用户名：" + decodedJWT.getClaim("username").asString());
        System.out.println("过期时间：" + decodedJWT.getExpiresAt());

    }

    /**
     * 解析token
     *
     * @param token
     */
    public AuthInfo parse(String token) {
        AuthInfo authInfo = tokenUtil.parseRefreshToken2(token);
        return authInfo;
    }

}
